<?php
namespace app\model;

use think\Model;
use think\facade\Db;


class BannerModel extends Model{
    protected $table   = 'sh_banner';
    public static function get_banner_info( $banner_id ){

        $sql = "select * from sh_banner where banner_id=" . $banner_id;



        // return pdo_fetch( $sql );
        return Db::query( $sql )[0];

    }

    public static function get_banner_image_list( $banner_id ){


        $sql = "select * from sh_banner_image where banner_id=" . $banner_id;


        return Db::query( $sql );
    }

    public static function get_list($filter = [])
    {

        global $_W;
        $uniacid = current_uni_account();
        $sql = "select * from sh_banner where uniacid=" . $uniacid;


        // return pdo_fetchall( $sql );
        return Db::query( $sql );
    }

    public static function banner_image_remove( $banner_id ){

        $sql = "delete from sh_banner_image where banner_id= " . $banner_id;

        // return pdo_fetchall( $sql );
        return Db::query( $sql );
    }
    public static function banner_image_set( $banner_id, $data ){

        $values = '';

        foreach( $data as $val ){

            $values .= "(" ;
            $values .= "'" . $banner_id . "',";
            $values .= "'" . $val['title'] . "',";
            $values .= "'" . $val['link'] . "',";
            $values .= "'" . $val['image'] . "',";
            $values .= "'" . $val['sort_order'] . "'";
            $values .=  "),";
        }

        $values = trim( $values , ',') ;
        $sql = "insert into sh_banner_image (`banner_id`,`title`,`link`,`image`,`sort_order`) values " . $values ;

        // return $sql; 
        // return pdo_query( $sql );
        return Db::execute( $sql );
    }

    public static function edit( $banner_id, $data ){

        $sql = "update sh_banner set `name`= '" . $data['name']
            . "', `status`=  " . $data['status']
            . ", `position`=  '" . $data['position']
            . "', `platform`=  '" . $data['platform']
            . "', `image_width`=  '" . $data['image_width'] ."'"
            . ", `image_height`=  '" . $data['image_height'] ."'"
            . " where banner_id=" . $banner_id;

        // return pdo_query( $sql );
        return Db::execute( $sql );
    }

    public static function create2( $data ){

        // global $_W;
        $uniacid = current_uni_account();
        $sql = "insert into sh_banner set `name`= '" . $data['name']
            . "', `uniacid`=  '" . $uniacid
            . "', `position`=  '" . $data['position']
            . "', `platform`=  '" . $data['platform']
            . "', `image_width`=  '" . $data['image_width']
            . "', `image_height`=  '" . $data['image_height']
            . "', `status`=  " . $data['status'];


        // pdo_query( $sql );
        // return pdo_insertid();
        Db::execute( $sql );
        return Db::name('sh_banner')->getLastInsID();
    }


    public static function remove( $banner_id ){

        $sql = "delete from sh_banner "
            . " where banner_id=" . $banner_id;

        // return pdo_query( $sql );
        return Db::execute( $sql );
    }
    public static function image_list_remove( $banner_id ){

        $sql = "delete from sh_banner_image "
            . " where banner_id=" . $banner_id;

        // return pdo_query( $sql );
        return Db::execute( $sql );
    }

}